package App.Job;

import App.Model.BaseEnum;
import App.Model.Chat.CrmChatGroup;
import App.Model.Task.CrmTask;
import App.Model.Task.CrmTaskMember;
import App.Model.Task.CrmTaskReminder;
import App.Repository.ChatRepository;
import App.Repository.XinGePushRepository;
import App.Service.TecentIMService;
import App.Service.XingeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by xw on 2019/4/23.
 */
@Slf4j
@Component
@EnableScheduling
public class TaskClearChatGroupJob{


    private XingeService _xingeService = new XingeService();
    @Autowired
    private ChatRepository _chatRepository;
    @Autowired
    private TecentIMService _imService;
//    @Scheduled(cron = "0 0 0 * * ?")
    public void execute() {
        try {
            if(1<2){

                return;
            }
            log.info("清理多余群聊任务开始：{}", DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));

            List<CrmChatGroup> chatGroups = _chatRepository.getCrmGroupByStatus();

            if(!CollectionUtils.isEmpty(chatGroups)){
                for (CrmChatGroup chatGroup : chatGroups) {
                    try{
                        // 这里的userid没有实际的用处
                        boolean result = _imService.destroyIMGroup(chatGroup.getGroupid(),0);
                        // 调用im接口解散群聊
                        if(result){
                            _chatRepository.updateGroupRecstatus(chatGroup.getGroupid());
                        }
                    }catch (Exception e){

                    }
                }
            }

            log.info("清理多余群聊任务完成：{}", DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        } catch (Exception ex) {
            log.error("清理多余群聊任务异常：{}", ex);
//            ex.printStackTrace();
        }
    }


    private Date getNewBeginTime(Calendar cal, int _aheadValue, int _aheadtype) {
        if (_aheadtype == BaseEnum.TaskAheadType.Minute.getTaskAheadType()) {
            cal.add(Calendar.MINUTE, -_aheadValue);
        } else if (_aheadtype == BaseEnum.TaskAheadType.Hour.getTaskAheadType()) {
            cal.add(Calendar.HOUR, -_aheadValue);
        } else if (_aheadtype == BaseEnum.TaskAheadType.Day.getTaskAheadType()) {
            cal.add(Calendar.DATE, -_aheadValue);
        } else if (_aheadtype == BaseEnum.TaskAheadType.Month.getTaskAheadType()) {
            cal.add(Calendar.MONTH, -_aheadValue);
        }
        return cal.getTime();
    }
}